/*
 * load or save the configure information
 */
package env;

import Relationship.Simple.DataTypes.Attribute;
import Relationship.Complex.DataTypes.ComplexRel;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.HashSet;

/**
 * Manage environment parameters.
 *
 * @author Jianwei "Chris" Leng, 2010-01-15 4:12pm @ WIlliams Building, University of Utah
 * @version 1.00
 * @since Dec 20, 2009
 * @description: Manager Configuration Variables, read variables to memory or update them to the config file
 */
public class Configure
{

    // filename of the configure file
    // path is current working path of eHOST
    private final static String CONF_FILE_NAME = "nlpconf.ini";

    /** read all configuration parameters for nlpconf.ini */
    static public void MUTEDMUTED_readAll() {

        //System.out.println("#eHOST# Loading configure file!");

        boolean iniFileExists = commons.Filesys.checkFilesExists(CONF_FILE_NAME);

        boolean isDirectory;

        if (!iniFileExists) {
            System.out.println("Lost configation file: nlpconf.ini !!!!! ");
            return;
        }

        isDirectory = commons.Filesys.isFileOrDirectory(CONF_FILE_NAME);// if it's a directory, show error and return
        if (isDirectory) {
            System.out.println("Lost configation file: nlpconf.ini - it's a directory!!!!! ");
            return;
        }

        // if the ini file exists, get the configuration parameters
        try {
            

            //ResultEditor.AnnotationClasses.ClassColor.clear();

            BufferedReader iniFile = new BufferedReader(new FileReader(CONF_FILE_NAME));
            String line = iniFile.readLine();

            while (line != null) {

                String[] str = line.split(":=", 2);
                if (str.length != 2) {
                    line = iniFile.readLine();
                    continue;
                }
                
                if (str[0] == null) {
                    line = iniFile.readLine();
                    continue;
                }
                
                if (str[0].trim().length() < 1) {
                    line = iniFile.readLine();
                    continue;
                }

                

                



                



                


                



                
                
                
                
                
                

                

                
                



                



                
                //System.out.println(str[1].trim() ); //debug - echo

                // read next line if exists
                line = iniFile.readLine();
            }
            iniFile.close();

            //CLASSCOLOR := test@@@@orange
            //CLASSCOLOR := problem@@@@blue
            //CLASSCOLOR := treatment@@@@red
            //CLASSCOLOR := ssn@@@@pink
            //CLASSCOLOR := date@@@@cyan
            /*if (ResultEditor.AnnotationClasses.ClassColor.getColorByClassname("test") == null)
            {
                ResultEditor.AnnotationClasses.ClassColor.addColorCode("test", "orange");
            }
            if (ResultEditor.AnnotationClasses.ClassColor.getColorByClassname("problem") == null)
            {
                ResultEditor.AnnotationClasses.ClassColor.addColorCode("problem", "pink");
            }
            if (ResultEditor.AnnotationClasses.ClassColor.getColorByClassname("treatment") == null)
            {
                ResultEditor.AnnotationClasses.ClassColor.addColorCode("treatment", "cyan");
            }
            if (ResultEditor.AnnotationClasses.ClassColor.getColorByClassname("ssn") == null)
            {
                ResultEditor.AnnotationClasses.ClassColor.addColorCode("ssn", "blue");
            }
            if (ResultEditor.AnnotationClasses.ClassColor.getColorByClassname("date") == null)
            {
                ResultEditor.AnnotationClasses.ClassColor.addColorCode("date", "red");
            }

            if (env.Parameters.getLatestUsedMentionID() < 10000)
            {
                env.Parameters.forceChangeLatestUsedMentionID(10000);
            }*/

        } catch (Exception e) {
            System.out.println("Configs.readAll() - " + e);
        }
        
        //System.out.println("#eHOST# Finished configure file loading!");

        //debug - echo
        //System.out.println("CONCEPT_LIB := " + nlp.switchpool.iniMSP.CONCEPT_LIB );
        //debug - echo
        //System.out.println("CONCEPT_LIB_SEPARATOR := "
        // + nlp.switchpool.iniMSP.CONCEPT_LIB_SEPARTOR );

    }

    

    /**This method is used to count quotation marks from strings. It usually
     * used in reload absolute path of latest used workspace. As we use quotation
     * marks to quota absolute path while saving it into confiugre file. So there
     * must only 2 quotation marks occurred in the string. So the right return
     * only is number 2;
     *
     * @return  2: there is only 2 quotation marks found in the string
     *          o.w. : failed
     * 
     */
    private static int MUTEDMUTED_countQuotationMarks(String _str){
        int count=0;
        try{
            if( _str==null)
                return 0;
            if( _str.trim().length()<1)
                return 0;
            

            char[] charray = _str.toCharArray();
            if(charray==null)
                return 0;

            int size=charray.length;
            for(int i=0; i<size; i++){
                if(charray[i]=='"'){
                    count++;                    
                }
            }
            
            
        }catch(Exception ex){
            System.out.println("error 1101141138:: fail to remove quotation " +
                    "marks from string."
                    + ex.getMessage() );
        }
        return count;
    }

    

    public static void MUTEDMUTED_updateAll() {
        try {
            FileOutputStream output = new FileOutputStream(CONF_FILE_NAME);

            PrintStream p = new PrintStream(output);
            String outstr;

            
            

            



            

            

            
            
            
            
            

            
            

            


            

            

            p.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /*
    // update the specified parameter value in file
    static public void updateConf( String _paraName, String _Value){
    // if the file not exist, create a new conf file
    boolean iniFileExists = nlp.commons.Filesys.checkFilesExists( CONF_FILE_NAME );
    if ( iniFileExists == false ) {

    }
    // update file by data in the conf-switch-pool

    }

    // create the conf file if it not existsa
    static public void createConfFileIfNotExist( String _fileNameWithFullPath ){

    }*/
}
